package com.zhangshuo.patternDesign.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by shuo.zhang on 2017-10-27.
 */
public class HeroManager {

    private static HeroManager instance = new HeroManager();
    private HeroManager(){}

    /**
     * 单例
     * @return
     */
    public static HeroManager getInstance(){
        return instance;
    }

    Map<String,AbstractHero> heroMap = new HashMap<>();

    /**
     * 获取英雄
     * @param name
     * @return
     */
    public AbstractHero getHero(String name){
        AbstractHero hero = heroMap.get(name);
        if(hero == null){
            synchronized (HeroManager.class){
                if(hero == null){
                    if("SF".equalsIgnoreCase(name))
                        hero = new SF();
                    if ("Lion".equalsIgnoreCase(name))
                        hero = new Lion();

                }
                if(hero !=null){
                    heroMap.put(name,hero);
                }
            }
        }
        if (hero ==null)
            throw new RuntimeException("创建英雄失败");

        return hero;
    }

}
